package com.makefuture.controller;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.upload.UploadFile;
import com.makefuture.model.TUser;
import com.makefuture.service.ExportService;
import com.makefuture.utils.ExcelUtil;

public class ExportController extends Controller {
	
	protected ExportService service = ExportService.service;
	
	/**
	 * 全字段导出
	 */
	public void exportExcel() {
		
		List<TUser> list = TUser.dao.find("select * from t_user");	// 查询数据
		
		// 导出
		service.export(getResponse(), getRequest(), list);		
		
		renderNull();
	}
	
	public void index(){
		
		render("index.jsp");
		
	}
	
	public void toimp() {
		render("import.jsp");
	}
	
	public void importExcel() {
		
		// 获取文件存入到服务器
		 UploadFile upFile = getFile("excel", "", 1024*1024*1024, "utf-8"); 
		 File f = upFile.getFile();
		 String cname = upFile.getUploadPath()+System.currentTimeMillis()+upFile.getFileName();
		 f.renameTo(new File(cname));
		//获取路径 读取 参考ExcelUtil 的main方法
		 System.out.println("rename:"+cname);
		 String[][] exceldata=null;
			File file = new File(cname);
			try {
				exceldata=ExcelUtil.getExcelData(file, 0);//0从第一行开始,如果要从第二行开始遍历,就写1.如果要从第三行开始遍历,就写2.
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			if(null!=exceldata){
				for (int i = 0; i < exceldata.length; i++) {
					String [] rowdata=exceldata[i];
					String lie1Date = rowdata[0];
					String lie2Date = rowdata[1];
					String lie3Date = rowdata[2];
					String lie4Date = rowdata[3];
					String lie5Date = rowdata[4];
					String lie6Date = rowdata[5];
					String lie7Date = rowdata[6];
					System.out.println("第"+(i+1)+"行,  第一列的值:"+lie1Date+" , 第二列的值:"+lie2Date+" , 第三列的值:"
					+lie3Date+" , 第四列的值:"+lie4Date+" , 第五列的值:"+lie5Date+" , 第六列的值:"+lie6Date+" , 第七列的值:"+lie7Date);
				}
		}
		
	}
}
